package com.ph.irp.base.incomeexpense.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ph.irp.base.incomeexpense.service.IncomeExpenseService;
import com.ph.irp.dto.IncomeExpenseDTO;
import com.ph.irp.dto.PageDTO;
import com.ph.util.StringUtil;
import com.ph.util.pagination.PageUtil;

public class IncomeExpenseAction extends ActionSupport {
	private String FORWARD = null;
	private HttpServletRequest request;
	private String incomeExpenseCode;
	private String incomeExpenseName;
	private String incomeExpenseDesc;
	private String accountBookName;
	private String incomeExpenseType;
	private String accountbookId;
	private List<IncomeExpenseDTO> incomeExpenseList;
	private IncomeExpenseDTO editedIncomeExpense;

	private IncomeExpenseService incomeExpenseService;
	private PageDTO page;
	private String cpage;

	public String getAccountbookId() {
		return accountbookId;
	}

	public void setAccountbookId(String accountbookId) {
		this.accountbookId = accountbookId;
	}

	public PageDTO getPage() {
		return page;
	}

	public void setPage(PageDTO page) {
		this.page = page;
	}

	public String getCpage() {
		return cpage;
	}

	public void setCpage(String cpage) {
		this.cpage = cpage;
	}

	public String getIncomeExpenseType() {
		return incomeExpenseType;
	}

	public void setIncomeExpenseType(String incomeExpenseType) {
		this.incomeExpenseType = incomeExpenseType;
	}

	public String getIncomeExpenseDesc() {
		return incomeExpenseDesc;
	}

	public void setIncomeExpenseDesc(String incomeExpenseDesc) {
		this.incomeExpenseDesc = incomeExpenseDesc;
	}

	public String getAccountBookName() {
		return accountBookName;
	}

	public void setAccountBookName(String accountBookName) {
		this.accountBookName = accountBookName;
	}

	public IncomeExpenseService getIncomeExpenseService() {
		return incomeExpenseService;
	}

	public void setIncomeExpenseService(
			IncomeExpenseService incomeExpenseService) {
		this.incomeExpenseService = incomeExpenseService;
	}

	public List<IncomeExpenseDTO> getIncomeExpenseList() {
		return incomeExpenseList;
	}

	public void setIncomeExpenseList(List<IncomeExpenseDTO> incomeExpenseList) {
		this.incomeExpenseList = incomeExpenseList;
	}

	public String showIncomeExpenseList() throws Throwable {

		incomeExpenseList = incomeExpenseService.getAllIncomeExpense();

		if (incomeExpenseList != null) {
			page = PageUtil.generatePageDTO(incomeExpenseList,
					"showIncomeExpenseList.action", cpage);
			incomeExpenseList = page.getResList();
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String addIncomeExpense() throws Throwable {
//		accountbookId
		IncomeExpenseDTO ieDTO = new IncomeExpenseDTO();
		ieDTO.setAccountbookId(accountbookId);
		ieDTO.setAccountBookName(accountBookName);
		ieDTO.setIncomeExpenseCode(incomeExpenseCode);
		ieDTO.setIncomeExpenseDesc(incomeExpenseDesc);
		ieDTO.setIncomeExpenseName(incomeExpenseName);
		ieDTO.setIncomeExpenseType(incomeExpenseType);
		incomeExpenseService.addIncomeExpense(ieDTO);
		FORWARD = "success";

		return FORWARD;
	}

	public String showEditIncomeExpense() throws Throwable {
		request = ServletActionContext.getRequest();
		String editIncomeExpenseID = (String) request
				.getParameter("editIncomeExpenseID");
		editedIncomeExpense = new IncomeExpenseDTO();

		editedIncomeExpense = incomeExpenseService
				.getIncomeExpenseByIncomeExpenseId(editIncomeExpenseID);
		if (editedIncomeExpense != null) {
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;

	}

	public String getIncomeExpenseByCondition() throws Throwable {

		if (incomeExpenseCode != null && incomeExpenseName != null) {
			incomeExpenseList = incomeExpenseService
					.getIncomeExpenseByNameAndCode(incomeExpenseCode,
							incomeExpenseName);
			page = PageUtil.generatePageDTO(incomeExpenseList,
					"getIncomeExpenseByCondition.action", cpage);
			incomeExpenseList = page.getResList();
		}

		if (incomeExpenseList != null) {
			FORWARD = "success";
		} else {
			FORWARD = "input";
		}

		return FORWARD;
	}

	public String editIncomeExpense() throws Throwable {

		incomeExpenseService.editIncomeExpense(editedIncomeExpense);
		FORWARD = "success";

		return FORWARD;
	}

	public String deleteIncomeExpense() throws Throwable {

		request = ServletActionContext.getRequest();
		String deletedIncomeExpenseID = (String) request
				.getParameter("deletedIncomeExpenseID");

		if (deletedIncomeExpenseID != null
				&& !deletedIncomeExpenseID.equals("")) {
			incomeExpenseService.deleteIncomeExpense(StringUtil
					.removeLastComma(deletedIncomeExpenseID));
		}

		FORWARD = "success";

		return FORWARD;
	}

	public String forbidIncomeExpense() throws Throwable {
		request = ServletActionContext.getRequest();
		String forbidIncomeExpenseID = (String) request
				.getParameter("forbidIncomeExpenseID");

		if (forbidIncomeExpenseID != null && !forbidIncomeExpenseID.equals("")) {
			incomeExpenseService.forbidIncomeExpense(StringUtil
					.removeLastComma(forbidIncomeExpenseID));
		}

		FORWARD = "success";

		return FORWARD;
	}

	public String antiForbidIncomeExpense() throws Throwable {
		request = ServletActionContext.getRequest();
		String forbidIncomeExpenseID = (String) request
				.getParameter("forbidIncomeExpenseID");

		if (forbidIncomeExpenseID != null && !forbidIncomeExpenseID.equals("")) {
			incomeExpenseService.antiForbidIncomeExpense(StringUtil
					.removeLastComma(forbidIncomeExpenseID));
		}

		FORWARD = "success";

		return FORWARD;
	}

	public String getIncomeExpenseCode() {
		return incomeExpenseCode;
	}

	public void setIncomeExpenseCode(String incomeExpenseCode) {
		this.incomeExpenseCode = incomeExpenseCode;
	}

	public String getIncomeExpenseName() {
		return incomeExpenseName;
	}

	public void setIncomeExpenseName(String incomeExpenseName) {
		this.incomeExpenseName = incomeExpenseName;
	}

	public void setEditIncomeExpense(IncomeExpenseDTO editIncomeExpense) {
		this.editedIncomeExpense = editIncomeExpense;
	}

	public IncomeExpenseDTO getEditedIncomeExpense() {
		return editedIncomeExpense;
	}

	public void setEditedIncomeExpense(IncomeExpenseDTO editedIncomeExpense) {
		this.editedIncomeExpense = editedIncomeExpense;
	}

}
